package com.atguigu.auth.service.impl;

import com.atguigu.auth.mapper.SysUserMapper;
import com.atguigu.auth.service.SysUserService;
import com.atguigu.common.jwt.JwtHelper;
import com.atguigu.common.result.Result;
import com.atguigu.common.utils.MD5;
import com.atguigu.model.system.SysUser;
import com.atguigu.security.custom.LoginUserInfoHelper;
import com.atguigu.vo.system.LoginVo;
import com.atguigu.vo.system.SysUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService {
    @Override
    public IPage<SysUser> pageQuery(Long page, Long limit, SysUserQueryVo vo) {
        IPage<SysUser> iPage = new Page<>(page,limit);
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(vo.getKeyword()),SysUser::getName,vo.getKeyword());

        page(iPage,new LambdaQueryWrapper<SysUser>()
                .like(StringUtils.hasText(vo.getKeyword()),SysUser::getUsername,vo.getKeyword())
                .le(StringUtils.hasText(vo.getCreateTimeEnd()),SysUser::getCreateTime, vo.getCreateTimeEnd())
                .ge(StringUtils.hasText(vo.getCreateTimeBegin()),SysUser::getCreateTime, vo.getCreateTimeBegin()));
        return iPage;
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        SysUser user = new SysUser();
        user.setId(id);
        user.setStatus(status);
        updateById(user);
    }

    @Override
    public Result login(LoginVo loginVo) {
        //登录的时候首先根据用户输入的用户名和密码
        //首先查询当前用户是否存在，如果存在就校验密码，如果密码也正确，就判断当前用户是否被禁用
        String username = loginVo.getUsername();
        String password = MD5.encrypt(loginVo.getPassword());
        SysUser user = getOne(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getUsername, username));
        if(user == null){
            return Result.fail("用户不存在");
        }
        if(!password.equals(user.getPassword())){
            return Result.fail("密码错误");
        }
        if(user.getStatus() == 0){
            return Result.fail("当前账号已被禁用");
        }
        //登陆成功后生成用户的jwt令牌并返回
        String jwt = JwtHelper.createToken(user.getId(), user.getUsername());
        Map<String,Object> map = new HashMap<>();
        map.put("token",jwt);
        return Result.ok(map);
    }

    @Override
    public Map<String, Object> getCurrentUser() {
        SysUser sysUser = getById(LoginUserInfoHelper.getUserId());
        Map<String, Object> map = new HashMap<>();
        map.put("name", sysUser.getName());
        map.put("phone", sysUser.getPhone());
        return map;
    }
}
